package com.hqq.dao;

import com.hqq.domain.WoocCardDetail;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * WoocCardDetailDao
 * Created by heqianqian on 2018/3/26.
 */
@Repository
public interface WoocCardDetailDao {

    String TABLE_NAME = "wooc_card_detail";
    String SELECT_FIELD = "card_detail_id as cardDetailId,card_detail_content as cardDetailContent,card_detail_user_id as cardDetailUserId," +
            "card_detail_create_time as cardDetailCreateTime,card_id as cardId";
    String INSERT_FIELD = "card_detail_content,card_detail_user_id,card_detail_create_time,card_id";

    /**
     * 根据帖子id查找评论
     *
     * @param cardId
     * @return
     */
    @Select("SELECT " + SELECT_FIELD + " FROM " + TABLE_NAME + " WHERE card_id = #{cardId}")
    List<WoocCardDetail> findByCardId(@Param("cardId") Long cardId);

    /**
     * 插入评论
     *
     * @param woocCardDetail
     */
    @SelectKey(before = false, statement = "SELECT LAST_INSERT_ID() AS cardDetailId", keyProperty = "cardDetailId", resultType = Long.class)
    @Insert("INSERT INTO " + TABLE_NAME + "(" + INSERT_FIELD + ") VALUES (#{cardDetailContent},#{cardDetailUserId},#{cardDetailCreateTime},#{cardId})")
    void saveWoocCard(WoocCardDetail woocCardDetail);

    /**
     * 删除评论
     *
     * @param cardId
     */
    @Delete("DELETE FROM " + TABLE_NAME + " WHERE card_id = #{cardId}")
    void deleteByCardId(@Param("cardId") Long cardId);
}
